package com.keyancompany.zhongbaopingtai.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.ConstansUrl;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.BaseAPI;
import com.keyancompany.zhongbaopingtai.api.FileAPI;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.Upload;
import com.keyancompany.zhongbaopingtai.utils.FileHelper;
import com.keyancompany.zhongbaopingtai.utils.T;

import java.io.File;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * 分享区域的上传文档
 */

public class UploadDocActivity extends BaseActivity {

    public static final int FILE_SELECT_CODE = 123;
    @BindString(R.string.create_share_file_text)
    String title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.select_file)
    TextView selectFile;
    @Bind(R.id.file_type_img)
    ImageView fileTypeImg;
    @Bind(R.id.file_name)
    TextView fileName;
    @Bind(R.id.file_info_parent)
    LinearLayout fileInfoParent;

    private File file;
    private String name;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_upload_doc;
    }

    @Override
    protected int getMenuId() {
        return R.menu.upload_menu;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (isNetvaiable()) {//不确定这个参数filedata是不是传这个
                    FileAPI.uploadFile(file, UploadDocActivity.this,
                            getLoading(R.string.watting_hint));
                }
                return false;
            }
        });
    }


    @Override
    public void success(int type, String json) {
        super.success(type, json);
        if (validateJson(json) != null) {

            BaseModel base = JSONObject.parseObject(json, BaseModel.class);
            Upload upload = JSONObject.parseObject(base.data, Upload.class);

            if (base.result.equals(Constants.SUCCESS)) {

            } else {
                T.showShort(mContext, base.msg);
            }
        } else {
            Log.e("TT", "上传成功！但是json为null");
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }


    /**
     * 调用文件选择软件来选择文件
     **/
    @OnClick(R.id.select_file)
    public void selectFile(View view) {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"),
                    FILE_SELECT_CODE);
        } catch (ActivityNotFoundException ex) {
            // Potentially direct the user to the Market with a Dialog
            T.showShort(mContext, "请安装文件管理器");
        }
    }


    @OnClick(R.id.file_delete)
    public void deleteFile(View view) {
        fileInfoParent.setVisibility(View.GONE);
        selectFile.setVisibility(View.VISIBLE);
    }

    /**
     * 根据返回选择的文件，来进行上传操作
     **/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode == Activity.RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            String url = FileHelper.getRealPath(mContext, uri);
            if (new File(url).exists()) {
                String filedata = url;
            }
            Log.e("TT", "获取文件路径 ：" + url);

            if (url == null) {
                T.showShort(mContext, "选择文件已损坏！请重新选择...");
                return;
            }
            name = url.substring(url.lastIndexOf("/") + 1);

            Log.e("TT", "获取文件路径 ：" + new File(url).exists() + " --- " + url.substring(url.lastIndexOf(".") + 1));
            if (new File(url).exists()) {
                file = new File(url);
            }

            fileInfoParent.setVisibility(View.VISIBLE);
            selectFile.setVisibility(View.GONE);
            fileName.setText(name);
            fileTypeImg.setImageResource(FileHelper.getFileIcon(name));


        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
